﻿
Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web


Public Class YearPlanProjectSP
    Inherits BusinessDomainBase

    Private _businessGUID As String     '业务GUID
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/qmys/YearPlanProjectSP.xml")
        _xml.LoadXml(MyBase.XML)
    End Sub

#Region "属性"
    '属性：业务GUID

    Public Overrides Property BusinessGUID() As String
        Get
            Return _businessGUID
        End Get
        Set(ByVal Value As String)
            _businessGUID = Value
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return _xml.OuterXml
        End Get
        Set(ByVal Value As String)
            _xml.LoadXml(Value)
        End Set
    End Property
#End Region


    Public Overrides Function Initiate() As String
        Dim strAry As String = MyDB.GetDataItemString("SELECT cast(Year as nvarchar(50))+'|'+cast(ObjectGUID as nvarchar(50))+'|'+cast(YearPlanGUID as nvarchar(50))  FROM ys_YearPlan2Object WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'")
        '预算年度
        Dim strPlanYear As String = strAry.Split("|")(0)
        '项目GUID
        Dim strObjectGUID As String = strAry.Split("|")(1)
        '版本GUID
        Dim strYearPlanGUID As String = strAry.Split("|")(2)

        Dim strSQL As String
        '更新年度预算计划2部门项目表数据，置为审核中状态
        strSQL = "UPDATE ys_YearPlan2Object SET " & _
                            "ItemState='审核中'" & _
                            ",ReportedByGUID='" & HttpContext.Current.Session("UserGUID") & " '" & _
                            ",ReportedBy='" & HttpContext.Current.Session("UserName") & " '" & _
                            ",ReportDate=getdate()" & _
                            ",PlanAmountYear=a.PlanAmount1+a.PlanAmount2+a.PlanAmount3+a.PlanAmount4+a.PlanAmount5+a.PlanAmount6+a.PlanAmount7+a.PlanAmount8+a.PlanAmount9+a.PlanAmount10+a.PlanAmount11+a.PlanAmount12" & _
                            ",PlanAmount1=a.PlanAmount1" & _
                            ",PlanAmount2=a.PlanAmount2" & _
                            ",PlanAmount3=a.PlanAmount3" & _
                            ",PlanAmount4=a.PlanAmount4" & _
                            ",PlanAmount5=a.PlanAmount5" & _
                            ",PlanAmount6=a.PlanAmount6" & _
                            ",PlanAmount7=a.PlanAmount7" & _
                            ",PlanAmount8=a.PlanAmount8" & _
                            ",PlanAmount9=a.PlanAmount9" & _
                            ",PlanAmount10=a.PlanAmount10" & _
                            ",PlanAmount11=a.PlanAmount11" & _
                            ",PlanAmount12=a.PlanAmount12" & _
                            " FROM (select sum(PlanAmount1) PlanAmount1,sum(PlanAmount2) PlanAmount2,sum(PlanAmount3) PlanAmount3,sum(PlanAmount4) PlanAmount4,sum(PlanAmount5) PlanAmount5,sum(PlanAmount6) PlanAmount6,sum(PlanAmount7) PlanAmount7,sum(PlanAmount8) PlanAmount8,sum(PlanAmount9) PlanAmount9,sum(PlanAmount10) PlanAmount10,sum(PlanAmount11) PlanAmount11,sum(PlanAmount12) PlanAmount12 from ys_YearPlanProjectExpend where YearNum='" & strPlanYear & "' and yearplanguid='" & strYearPlanGUID & "' and ProjGUID='" & strObjectGUID & "') a" & _
                            " WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "'"

        '提交更新语句
        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""上报失败，请稍后再试！""></xml>"
        End If

        '返回执行是否成功的信息
        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：取业务数据，填充XML
    Public Overrides Function LoadData() As Integer
        Dim strSQL As String
        Dim dtTemp As DataTable
        Dim dtTemp1 As DataTable
        '主记录    
        strSQL = "SELECT YearPlan2ObjectGUID,ys_YearPlan2Object.YearPlanGUID,ys_yearplan.PlanName, ObjectGUID,ProjCode,ProjName, Year, ReportedBy, ReportDate,convert(varchar(10),getdate(),120) as CurDate " & _
                 " FROM ys_YearPlan2Object Left Join p_project On ys_YearPlan2Object.ObjectGUID = p_project.ProjGUID " & _
                 " Left Join ys_yearplan On ys_YearPlan2Object.yearplanGUID = ys_yearplan.yearplanGUID " & _
                 " WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        dtTemp1 = MyDB.GetDataTable("SELECT isnull(sum(PlanAmount1+PlanAmount2+PlanAmount3+PlanAmount4+PlanAmount5+PlanAmount6+PlanAmount7+PlanAmount8+PlanAmount9+PlanAmount10+PlanAmount11+PlanAmount12),0) PlanAmount " & _
                                    " FROM ys_YearPlanProjectExpend a " & _
                                    " WHERE a.YearNum='" & dtTemp.Rows(0).Item("Year").ToString & "' and yearplanguid='" & dtTemp.Rows(0).Item("YearPlanGUID").ToString & "' and ProjGUID='" & dtTemp.Rows(0).Item("ObjectGUID").ToString & "'")

        If dtTemp.Rows.Count > 0 Then
            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年度预算计划2部门GUID']").InnerText = dtTemp.Rows(0).Item("YearPlan2ObjectGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目GUID']").InnerText = dtTemp.Rows(0).Item("ObjectGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='项目编码']").InnerText = dtTemp.Rows(0).Item("ProjCode").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报项目']").InnerText = dtTemp.Rows(0).Item("ProjName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年度']").InnerText = dtTemp.Rows(0).Item("Year").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='版本GUID']").InnerText = dtTemp.Rows(0).Item("YearPlanGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='版本']").InnerText = dtTemp.Rows(0).Item("PlanName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报人']").InnerText = HttpContext.Current.Session("UserName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报日期']").InnerText = dtTemp.Rows(0).Item("CurDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报金额']").InnerText = Format(dtTemp1.Rows(0).Item("PlanAmount"), "#,##0.00")
        End If

        Return 0
    End Function

    '功能：根据XML进行数据归档
    Public Overrides Function SaveData() As String
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = PublicMNG.GetApproveSeparator("审批通过")
        Dim strSQL As String

        strSQL = "UPDATE ys_YearPlan2Object SET ItemState='已审核'" & _
                        ",ApprovedBy='" & HttpContext.Current.Session("UserName") & "'" & _
                        ",ApproveDate=GetDate()" & _
                        ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
                        " WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "';"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""归档失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = PublicMNG.GetApproveSeparator("审批不通过")
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ItemState='未审核',ReportedByGUID=NULL,ReportedBy=NULL,ReportDate=NULL" & _
               ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批不通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
               ",PlanAmountYear=0.00" & _
               ",PlanAmount1=0.00" & _
               ",PlanAmount2=0.00" & _
               ",PlanAmount3=0.00" & _
               ",PlanAmount4=0.00" & _
               ",PlanAmount5=0.00" & _
               ",PlanAmount6=0.00" & _
               ",PlanAmount7=0.00" & _
               ",PlanAmount8=0.00" & _
               ",PlanAmount9=0.00" & _
               ",PlanAmount10=0.00" & _
               ",PlanAmount11=0.00" & _
               ",PlanAmount12=0.00" & _
               " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"

    End Function
    '功能：作废
    Public Overrides Function SaveData_Cancel() As String
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = PublicMNG.GetApproveSeparator("审批作废")
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ItemState='未审核',ReportedByGUID=NULL,ReportedBy=NULL,ReportDate=NULL" & _
              ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批作废" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
              ",PlanAmountYear=0.00" & _
              ",PlanAmount1=0.00" & _
              ",PlanAmount2=0.00" & _
              ",PlanAmount3=0.00" & _
              ",PlanAmount4=0.00" & _
              ",PlanAmount5=0.00" & _
              ",PlanAmount6=0.00" & _
              ",PlanAmount7=0.00" & _
              ",PlanAmount8=0.00" & _
              ",PlanAmount9=0.00" & _
              ",PlanAmount10=0.00" & _
              ",PlanAmount11=0.00" & _
              ",PlanAmount12=0.00" & _
              " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：审批通过后再驳回数据处理
    Public Function SaveData_RollBack() As String
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = PublicMNG.GetApproveSeparator("审批驳回")
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ItemState='未审核',ReportedByGUID=NULL,ReportedBy=NULL,ReportDate=NULL" & _
              ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批驳回" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
              ",PlanAmountYear=0.00" & _
              ",PlanAmount1=0.00" & _
              ",PlanAmount2=0.00" & _
              ",PlanAmount3=0.00" & _
              ",PlanAmount4=0.00" & _
              ",PlanAmount5=0.00" & _
              ",PlanAmount6=0.00" & _
              ",PlanAmount7=0.00" & _
              ",PlanAmount8=0.00" & _
              ",PlanAmount9=0.00" & _
              ",PlanAmount10=0.00" & _
              ",PlanAmount11=0.00" & _
              ",PlanAmount12=0.00" & _
              " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：发起撤回
    Public Overrides Function InitiateWithDrawn() As String
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = PublicMNG.GetApproveSeparator("审批不通过")
        Dim strSQL As String
        strSQL = "UPDATE ys_YearPlan2Object SET ItemState='未审核',ReportedByGUID=NULL,ReportedBy=NULL,ReportDate=NULL" & _
              ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批不通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
              ",PlanAmountYear=0.00" & _
              ",PlanAmount1=0.00" & _
              ",PlanAmount2=0.00" & _
              ",PlanAmount3=0.00" & _
              ",PlanAmount4=0.00" & _
              ",PlanAmount5=0.00" & _
              ",PlanAmount6=0.00" & _
              ",PlanAmount7=0.00" & _
              ",PlanAmount8=0.00" & _
              ",PlanAmount9=0.00" & _
              ",PlanAmount10=0.00" & _
              ",PlanAmount11=0.00" & _
              ",PlanAmount12=0.00" & _
              " WHERE YearPlan2ObjectGUID = '" & Me._businessGUID & "'"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

End Class
